﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;



namespace AtmosCreator
{
    class LanguageControl
    {

    

        /// <summary>
        /// Instance of the configuration file
        /// </summary>
        private LanguageFile _file;

        public LanguageControl()
        {
            DeserializeConf();
        }

        /// <summary>
        /// Deserialize the language file
        /// </summary>
        private void DeserializeConf()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(LanguageFile));
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(AtmosCreator.Properties.Resources.LanguageFilename, FileMode.Open);
                _file = (LanguageFile)serializer.Deserialize(fileStream);
            }
            finally
            {
                fileStream.Close();
            }
        }


        /// <summary>
        /// Save the language to hard disk
        /// </summary>
        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(LanguageFile));
            StreamWriter writer = new StreamWriter(AtmosCreator.Properties.Resources.LanguageFilename);
            serializer.Serialize(writer, _file);
            writer.Close();
        }


        /// <summary>
        /// Creates a default language file
        /// </summary>
        public static void CreateDefaultLanguage()
        {
            LanguageFile lang = new LanguageFile();
            lang.Language = "english";
            XmlSerializer serializer = new XmlSerializer(typeof(LanguageFile));
            StreamWriter writer = new StreamWriter(AtmosCreator.Properties.Resources.LanguageFilename);
            serializer.Serialize(writer, lang);
            writer.Close();
        }

        /// <summary>
        /// get and set methods
        /// </summary>
        public string Language
        {
            get
            {
                return _file.Language;
            }
            set
            {
                _file.Language = value;
            }
        }
    }
}
